<?php
/*
// OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
// all the essential functionalities required for any enterprise.
// Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com

// OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
// the GNU General Public License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.

// OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along with this program;
// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA  02110-1301, USA
*/

require_once ROOT_PATH . '/lib/confs/Conf.php';
require_once ROOT_PATH . '/lib/confs/sysConf.php';
require_once ROOT_PATH . '/lib/dao/DMLFunctions.php';
require_once ROOT_PATH . '/lib/dao/SQLQBuilder.php';
require_once ROOT_PATH . '/lib/common/CommonFunctions.php';

class AdministraMana {

	var $db;

	public function __construct() {
		$this->db = new DMLFunctions();
		error_reporting(0);
	}

	public function __distruct() {
		$this->db->dbDisconnect();
	}
	
	/**
	 *获取搜索的sqlwhere 
	 **/
	public function GetSearchSlqWhere($code="",$name="",$table="") {
		$where = "";
		$loc_code = isset($_POST['loc_code'])?htmlspecialchars(trim($_POST['loc_code'])):"";
		$loc_name = isset($_POST['loc_name'])?htmlspecialchars(trim($_POST['loc_name'])):"";
		if($code != "") $loc_code = $code;
		if($name != "") $loc_name = $name;
		if($loc_name != "") {
			if($loc_name == "emp_firstname") {
				$where .= " and {$loc_code} like '%{$loc_name}%' ";
			} else {
				$where .= " and {$loc_code} = '{$loc_name}' ";
			}
		}
		$Year = intval($_REQUEST['Year']);
		$Month = intval($_REQUEST['Month']);
		if($Year > 0 && $Month > 0) {
			/*$Year = date("Y");
			 $Month = intval(date("m"));*/
			if($table == "hs_hr_emp_bonuspenalty") {
				$where .= " and wordYear='".$Year."' and wordMonth = '".$Month."' ";
			} else {
				$where .= " and FROM_UNIXTIME(AddTime,'%Y%c')='".$Year.$Month."' ";
			}
		}
		return $where;
	}
	
	public function BonusPenaltyDo($action="",$getArr="",$postArr="") {
		if($action == "addmode") {
			return $this->getOneAdministraMana($getArr['Id'],"hs_hr_emp_bonuspenalty");
		} elseif($action == "DeleteMode") {
			$this->delEmpAdministraMana($postArr,"hs_hr_emp_bonuspenalty");
			return $this->getListofAdministraMana($page,"hs_hr_emp_bonuspenalty",array(1,2));
		} elseif($action == "updatemode") {
		    $this->SaveBonusPenalty($getArr,$postArr);
			return $this->getListofAdministraMana($page,"hs_hr_emp_bonuspenalty",array(1,2));
		} elseif($action == "shenhemode") {
		    $this->SaveBonusPenaltySh($getArr,$postArr);
			return $this->getListofAdministraMana($page,"hs_hr_emp_bonuspenalty",array(1,2));
		} else {
			return $this->getListofAdministraMana($page,"hs_hr_emp_bonuspenalty",array(1,2));
		}
	}
	
	/**
	 *积假管理 
	 **/
	public function AnnualLeaveDo($action="",$getArr="",$postArr="") {
		if($action == "addmode") {
			return $this->getOneAnnualLeave($getArr['Id'],"hs_hr_emp_annualleave");
		} elseif($action == "DeleteMode") {
			$this->delEmpAnnualLeave($postArr,"hs_hr_emp_annualleave");
			return $this->getListofAnnualLeave($page,"hs_hr_emp_annualleave");
		} elseif($action == "updatemode") {
		    $this->SaveAnnualLeave($getArr,$postArr);
			return $this->getListofAnnualLeave($page,"hs_hr_emp_annualleave");
		} else {
			return $this->getListofAnnualLeave($page,"hs_hr_emp_annualleave");
		}
	}
	
	public function SalaryAdjustmentsDo($action="",$getArr="",$postArr="") {
		/*if($action == "addmode") {
			return $this->getOneAdministraMana($getArr['Id'],"hs_hr_emp_salary_adjustments");
		} elseif($action == "DeleteMode") {
			$this->delEmpAdministraMana($postArr,"hs_hr_emp_salary_adjustments");
			return $this->getListofAdministraMana($page,"hs_hr_emp_salary_adjustments",array(1,2));
		} elseif($action == "updatemode") {
		    $this->SaveBonusPenalty($getArr,$postArr);
			return $this->getListofAdministraMana($page,"hs_hr_emp_salary_adjustments",array(1,2));
		} else {
			return $this->getListofAdministraMana($page,"hs_hr_emp_salary_adjustments",array(1,2));
		}*/
		return $this->getListofAdministraMana($page,"hs_hr_emp_salary_adjustments",array(1,2));
	}
	
	public function SocialSecurityDo($action="",$getArr="",$postArr="") {
			return $this->getListSocialSecurity($page,$getArr,$postArr);
	}
	
	/**
	 *领取物品记录 
	 **/
	public function ReceiveGoodsDo($action="",$getArr="",$postArr="") {
		if($action == "addmode") {
			return $this->getOneAdministraMana($getArr['Id'],"hs_hr_emp_goodsrecord");
		} elseif($action == "DeleteMode") {
			$this->delEmpAdministraMana($postArr,"hs_hr_emp_goodsrecord");
			return $this->getListofAdministraMana($page,"hs_hr_emp_goodsrecord");
		} elseif($action == "updatemode") {
		    $this->SaveReceiveGoods($getArr,$postArr);
			return $this->getListofAdministraMana($page,"hs_hr_emp_goodsrecord");
		} else {
			return $this->getListofAdministraMana($page,"hs_hr_emp_goodsrecord");
		}
	}
	
	/**
	 *培训记录 
	 **/
	public function TrainRecordDo($action="",$getArr="",$postArr="") {
		if($action == "addmode") {
			return $this->getOneAdministraMana($getArr['Id'],"hs_hr_emp_train");
		} elseif($action == "DeleteMode") {
			$this->delEmpAdministraMana($postArr,"hs_hr_emp_train");
			return $this->getListofAdministraMana($page,"hs_hr_emp_train");
		} elseif($action == "updatemode") {
		    $this->SaveTrainRecord($getArr,$postArr);
			return $this->getListofAdministraMana($page,"hs_hr_emp_train");
		} else {
			return $this->getListofAdministraMana($page,"hs_hr_emp_train");
		}
	}
	
	
	/**
	 *员工宿舍记录 
	 **/
	public function DormitoryDo($action="",$getArr="",$postArr="") {
		if($action == "addmode") {
			return $this->getOneAdministraMana($getArr['Id'],"hs_hr_emp_dorm");
		} elseif($action == "DeleteMode") {
			$this->delEmpAdministraMana($postArr,"hs_hr_emp_dorm");
			return $this->getListofAdministraMana($page,"hs_hr_emp_dorm");
		} elseif($action == "updatemode") {
		    $this->SaveDormitory($getArr,$postArr);
			return $this->getListofAdministraMana($page,"hs_hr_emp_dorm");
		} else {
			return $this->getListofAdministraMana($page,"hs_hr_emp_dorm");
		}
	}
	
	
	
	function getListSocialSecurity($page="",$getArr="",$postArr='') {

		$sysConst = new sysConf();
		$where = "Where 1=1 ";
		$where .= $this->GetSearchSlqWhere();

        $limit = "";
        $list = array();
		$pageNO = isset($_POST['pageNO'])?intval($_POST['pageNO']):1;
        if ($pageNO > 0) {
            $pageNO--;
            $pageNO *= $sysConst->itemsPerPage;

            $limit = " limit {$pageNO}, {$sysConst->itemsPerPage}";
        }
        
		$sql = "select count(a.emp_number) as allcount from hs_hr_emp_basicsalary a left join hs_hr_employee b on b.emp_number = a.emp_number {$where}";
		$query = $this->db->executeQuery($sql);
		$Allcount = mysql_fetch_array($query, MYSQL_ASSOC);
		$arrayDispList = '';
		$sqlQString = "select a.*,b.emp_lastname,b.emp_firstname,b.work_station,b.employee_id from hs_hr_emp_basicsalary a left join hs_hr_employee b on b.emp_number=a.emp_number {$where} {$limit}";

		$message2 = $this->db->executeQuery($sqlQString); //Calling the addData() function

		 while ($line = mysql_fetch_array($message2, MYSQL_ASSOC)) {
	    	$arrayDispList[] = $line;
	     }
	    
		return array('count'=>$Allcount,'list'=>$arrayDispList);
	}
	
	
	function getListofAdministraMana($page,$table,$TypeArr='') {

		$sysConst = new sysConf();
		$where = "Where 1=1 ";
		$where .= $this->GetSearchSlqWhere('','',$table);
		$typeid = $_GET['typeid'];
		if($typeid != "") {
			$where .= " and TypeId in ({$typeid}) ";
		} elseif(!empty($TypeArr)) {
			$where .= " and TypeId in (".implode(',',$TypeArr).") ";
		}
        $limit = "";
        $list = array();
		$pageNO = isset($_POST['pageNO'])?intval($_POST['pageNO']):1;
        if ($pageNO > 0) {
            $pageNO--;
            $pageNO *= $sysConst->itemsPerPage;

            $limit = " limit {$pageNO}, {$sysConst->itemsPerPage}";
        }
        
		$sql = "select count(a.Id) as allcount from {$table} a left join hs_hr_employee b on b.emp_number = a.EmpId {$where}";
		$query = $this->db->executeQuery($sql);
		$Allcount = mysql_fetch_array($query, MYSQL_ASSOC);
		$arrayDispList = '';
		$sqlQString = "select a.*,b.emp_lastname,b.emp_firstname,b.work_station,b.employee_id from {$table} a left join hs_hr_employee b on b.emp_number=a.EmpId {$where} {$limit}";

		$message2 = $this->db->executeQuery($sqlQString); //Calling the addData() function

		 while ($line = mysql_fetch_array($message2, MYSQL_ASSOC)) {
	    	$arrayDispList[] = $line;
	     }
	    
		return array('count'=>$Allcount,'list'=>$arrayDispList);
	}
	
	
	function getListofAnnualLeave($page,$table,$TypeArr='') {

		$sysConst = new sysConf();
		$where = "Where 1=1 ";
		$where .= $this->GetSearchSlqWhere('','',$table);
		$typeid = $_GET['typeid'];
		if($typeid != "") {
			$where .= " and TypeId in ({$typeid}) ";
		} elseif(!empty($TypeArr)) {
			$where .= " and TypeId in (".implode(',',$TypeArr).") ";
		}
        $limit = "";
        $list = array();
		$pageNO = isset($_POST['pageNO'])?intval($_POST['pageNO']):1;
        if ($pageNO > 0) {
            $pageNO--;
            $pageNO *= $sysConst->itemsPerPage;

            $limit = " limit {$pageNO}, {$sysConst->itemsPerPage}";
        }
        
		$sql = "select count(a.EmpId) as allcount from {$table} a left join hs_hr_employee b on b.emp_number = a.EmpId {$where}";
		$query = $this->db->executeQuery($sql);
		$Allcount = mysql_fetch_array($query, MYSQL_ASSOC);
		$arrayDispList = '';
		$sqlQString = "select a.*,b.emp_lastname,b.emp_firstname,b.work_station,b.employee_id from {$table} a left join hs_hr_employee b on b.emp_number=a.EmpId {$where} {$limit}";

		$message2 = $this->db->executeQuery($sqlQString); //Calling the addData() function

		 while ($line = mysql_fetch_array($message2, MYSQL_ASSOC)) {
	    	$arrayDispList[] = $line;
	     }
	    
		return array('count'=>$Allcount,'list'=>$arrayDispList);
	}
	

	function getOneAdministraMana($Id = "",$table) {

		if($Id != "") {
		$sqlQString = "select a.*,b.emp_lastname,b.emp_firstname from {$table} a left join hs_hr_employee b on b.emp_number = a.EmpId where a.Id = {$Id}";

		$dbConnection = new DMLFunctions();
		$message = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		$i=0;

		$arrayDispList = mysql_fetch_array($message, MYSQL_ASSOC);
		
		}


	     if (isset($arrayDispList)) {

			return $arrayDispList;

		} else {

			$arrayDispList = '';
			return $arrayDispList;

		}
	}

	function getOneAnnualLeave($EmpId = "",$table) {

		if($EmpId != "") {
		$sqlQString = "select a.*,b.emp_lastname,b.emp_firstname from {$table} a left join hs_hr_employee b on b.emp_number = a.EmpId where a.EmpId = {$EmpId}";

		$dbConnection = new DMLFunctions();
		$message = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		$i=0;

		$arrayDispList = mysql_fetch_array($message, MYSQL_ASSOC);
		
		}


	     if (isset($arrayDispList)) {

			return $arrayDispList;

		} else {

			$arrayDispList = '';
			return $arrayDispList;

		}
	}


	function delEmpAdministraMana($post,$tableName) {

		$arrFieldList[0] = 'Id';

		$sql_builder = new SQLQBuilder();

		$sql_builder->table_name = $tableName;
		$sql_builder->flg_delete = 'true';
		$sql_builder->arr_delete = $arrFieldList;
		$arrList[0] =  $post['chkLocID'];
		$sqlQString = $sql_builder->deleteRecord($arrList);
		//echo $sqlQString;
		//die();
		$dbConnection = new DMLFunctions();
		$message2 = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		return $message2;
	}


	function delEmpAnnualLeave($post,$tableName) {

		$arrFieldList[0] = 'EmpId';

		$sql_builder = new SQLQBuilder();

		$sql_builder->table_name = $tableName;
		$sql_builder->flg_delete = 'true';
		$sql_builder->arr_delete = $arrFieldList;
		$arrList[0] =  $post['chkLocID'];
		$sqlQString = $sql_builder->deleteRecord($arrList);
		//echo $sqlQString;
		//die();
		$dbConnection = new DMLFunctions();
		$message2 = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		return $message2;
	}
	
	
	function SaveBonusPenalty($getArr,$postArr) {

		if(intval($postArr['Id']) > 0) {
			$sqlQString = "update hs_hr_emp_bonuspenalty set EmpId = {$postArr['EmpId']},TypeId={$postArr['TypeId']},BonusPenaltyMoney='{$postArr['BonusPenaltyMoney']}',Content='{$postArr['Content']}',wordYear='{$postArr['wordYear']}',wordMonth='{$postArr['wordMonth']}',Mender='{$_SESSION['fname']}',MenderTime=".time()." where Id={$postArr['Id']}";
		} else {
			$sqlQString = "insert into hs_hr_emp_bonuspenalty (EmpId,TypeId,BonusPenaltyMoney,Content,wordYear,wordMonth,Addtime,Writer) VALUES ({$postArr['EmpId']},'{$postArr['TypeId']}','{$postArr['BonusPenaltyMoney']}','{$postArr['Content']}','{$postArr['wordYear']}','{$postArr['wordMonth']}',".time().",'{$_SESSION['fname']}') ";
		}

		$dbConnection = new DMLFunctions();
		$message = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		return $message;

	}
	
	function SaveBonusPenaltySh($getArr,$postArr) {
	
		if(intval($getArr['Id']) > 0) {
			$sqlQString = "update hs_hr_emp_bonuspenalty set IsCheck = 1,Assessor='{$_SESSION['fname']}',CheckTime=".time()." where Id={$getArr['Id']}";
		}
	
		$dbConnection = new DMLFunctions();
		$message = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function
	
		return $message;
	
	}

	/**
	 *保存积假 
	 **/
	function SaveAnnualLeave($getArr,$postArr) {
		
		if(intval($postArr['EmpId']) > 0) {
			$dbConnection = new DMLFunctions();
			$selSql = "select EmpId from hs_hr_emp_annualleave where EmpId={$postArr['EmpId']}";
			$query = $dbConnection -> executeQuery($selSql);
			$res = mysql_fetch_array($query, MYSQL_ASSOC);
			if($res) {
				$sqlQString = "update hs_hr_emp_annualleave set TypeId=0,LeaveHours='{$postArr['LeaveHours']}',ResidualHours='{$postArr['ResidualHours']}',Remark='',EffectDate='{$postArr['EffectDate']}',EndDate='{$postArr['EndDate']}',Assessor='{$_SESSION['fname']}',CheckTime=".time()." where EmpId={$postArr['EmpId']}";
			}else {
				$sqlQString = "insert into hs_hr_emp_annualleave (EmpId,TypeId,LeaveHours,ResidualHours,Remark,EffectDate,EndDate,Addtime,Writer) VALUES ({$postArr['EmpId']},'0','{$postArr['LeaveHours']}','{$postArr['ResidualHours']}','','{$postArr['EffectDate']}','{$postArr['EndDate']}',".time().",'{$_SESSION['fname']}') ";
			}
			$message = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function
		} else {
			exit("你还没选中员工！");
		}

		return $message;

	}
	
	/**
	 *记录调薪 
	 **/
	function SaveSalaryAdjustments($postArr) {
		if($postArr['SalaryAfter'] >= $postArr['SalaryBefore']){
			$postArr['TypeId'] = 1;
		} else {
			$postArr['TypeId'] = 2;
		}
		$postArr['AdjustmentsMoney'] = abs($postArr['SalaryAfter']-$postArr['SalaryBefore']);
		$sqlQString = "insert into hs_hr_emp_salary_adjustments (EmpId,TypeId,MoldId,SalaryBefore,SalaryAfter,AdjustmentsMoney,Content,Addtime,Writer) VALUES ({$postArr['EmpId']},'{$postArr['TypeId']}','{$postArr['MoldId']}','{$postArr['SalaryBefore']}','{$postArr['SalaryAfter']}','{$postArr['AdjustmentsMoney']}','{$postArr['Content']}',".time().",'{$_SESSION['fname']}') ";
		
		$dbConnection = new DMLFunctions();
		$message = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		return $message;

	}
	
	function SaveReceiveGoods($getArr,$postArr) {
		if($postArr['GetTime'] != "") {
			 //$postArr['GetTime'] = strtotime($postArr['GetTime']);
			 $postArr['GetTime'] = strtotime($postArr['GetTime']." ".intval($postArr['GetTime_hour']).":".intval($postArr['GetTime_min']).":00");
		}
		if(intval($postArr['Id']) > 0) {
			$sqlQString = "update hs_hr_emp_goodsrecord set EmpId = {$postArr['EmpId']},`type`=1,Content='{$postArr['Content']}',GetTime='{$postArr['GetTime']}',Mender='{$_SESSION['fname']}',MenderTime=".time()." where Id={$postArr['Id']}";
		} else {
			$sqlQString = "insert into hs_hr_emp_goodsrecord (EmpId,`type`,Num,Content,GetTime,Writer,Addtime) VALUES ({$postArr['EmpId']},1,'1','{$postArr['Content']}','{$postArr['GetTime']}','{$_SESSION['fname']}',".time().") ";
		}

		$dbConnection = new DMLFunctions();
		$message = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		return $message;

	}

	function SaveTrainRecord($getArr,$postArr) {
		if($postArr['StartTime'] != "") $postArr['StartTime'] = strtotime($postArr['StartTime']);
		if(intval($postArr['Id']) > 0) {
			$sqlQString = "update hs_hr_emp_train set EmpId = {$postArr['EmpId']},`TypeName`='{$postArr['TypeName']}',Address='{$postArr['Address']}',StartTime='{$postArr['StartTime']}',Money='{$postArr['Money']}',Content='{$postArr['Content']}',Mender='{$_SESSION['fname']}',MenderTime=".time()." where Id={$postArr['Id']}";
		} else {
			$sqlQString = "insert into hs_hr_emp_train (EmpId,`TypeName`,Address,Money,Content,StartTime,Writer,Addtime) VALUES ({$postArr['EmpId']},'{$postArr['TypeName']}','{$postArr['Address']}','{$postArr['Money']}','{$postArr['Content']}','{$postArr['StartTime']}','{$_SESSION['fname']}',".time().") ";
		}
		$dbConnection = new DMLFunctions();
		$message = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		return $message;

	}


	function SaveDormitory($getArr,$postArr) {
		if(intval($postArr['Id']) > 0) {
			$sqlQString = "update hs_hr_emp_dorm set EmpId = {$postArr['EmpId']},`DormNum`='{$postArr['DormNum']}',EmpNum='{$postArr['EmpNum']}',DormAddress='{$postArr['DormAddress']}',DormAdmin='{$postArr['DormAdmin']}',phone='{$postArr['phone']}',Mender='{$_SESSION['fname']}',MenderTime=".time()." where Id={$postArr['Id']}";
		} else {
			$sqlQString = "insert into hs_hr_emp_dorm (EmpId,`DormNum`,EmpNum,DormAddress,DormAdmin,phone,Writer,Addtime) VALUES ({$postArr['EmpId']},'{$postArr['DormNum']}','{$postArr['EmpNum']}','{$postArr['DormAddress']}','{$postArr['DormAdmin']}','{$postArr['phone']}','{$_SESSION['fname']}',".time().") ";
		}
		$dbConnection = new DMLFunctions();
		$message = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		return $message;

	}
	
	
	function updateEmpAdministraMana() {

		$arrRecordsList[0] = "'". $this->getId() . "'";
		$arrRecordsList[1] = "'". $this->getEmpId() . "'";
		$arrRecordsList[2] = "'". $this->getNum() . "'";
		$arrRecordsList[3] = $this->getContent();
		$arrRecordsList[4] = "'". $this->getAddtime() . "'";

		$tableName = 'HS_HR_EMP_ADMINISTRATIVE';
		$arrFieldList[0] = 'Id';
		$arrFieldList[1] = 'EmpId';
		$arrFieldList[2] = 'Num';
		$arrFieldList[3] = 'Content';
		$arrFieldList[4] = 'Addtime';

		$sql_builder = new SQLQBuilder();

		$sql_builder->table_name = $tableName;
		$sql_builder->flg_update = 'true';
		$sql_builder->arr_update = $arrFieldList;
		$sql_builder->arr_updateRecList = $arrRecordsList;

		$sqlQString = $sql_builder->addUpdateRecord1(1);
	//echo  $sqlQString;
		$dbConnection = new DMLFunctions();
		$message2 = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		return $message2;
	}
	
	function getOneBonuspenalties($year,$month,$empid) {
		if($empid != "") {
			$sqlQString = "select BonusPenaltyMoney from hs_hr_emp_bonuspenalty where wordYear={$year} and wordMonth={$month} and EmpId={$empid} and IsCheck=1 and TypeId=1;";

			$dbConnection = new DMLFunctions();
			$message = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

			while($row=mysql_fetch_array($message, MYSQL_ASSOC)) {
				$arrayDispList[] = $row;
			}
		}


		if (isset($arrayDispList)) {
			return $arrayDispList;

		} else {
			$arrayDispList = '';
			return $arrayDispList;
		}
	}

}
?>